﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EasyUiProject.Model.EasyUI;
using EPanShareSystem.DbUtity;
using EPanShareSystem.Model;

namespace EPanShareSystem.BLL
{
   public class MenuManage
    {
       EFDbHelper  dbHelper=new EFDbHelper();

       public IEnumerable<Sys_Menu> GetOrgList()
       {
           try
           {
               string sql = @"select * from Sys_Menu";
               //  var list = dbHelper.FinList<View_Sys_User>(sql);
               var list = dbHelper.FinList<Sys_Menu>(sql).ToList();
               return list;
           }
           catch (Exception)
           {

               throw;
           }

       }


       public List<AceMenu> GetMenuTree()
       {
           var treeList = new List<AceMenu>();
           string sql = @"select * from Sys_Menu";
           //  var list = dbHelper.FinList<View_Sys_User>(sql);
           var orgs = dbHelper.FinList<Sys_Menu>(sql).ToList();
           if (orgs != null && orgs.Any())
           {
               var queryTreeRoots = orgs.Where(a => string.IsNullOrEmpty(a.ParentNodeId)).OrderBy(a => a.OrderID);
               foreach (var item in queryTreeRoots)
               {
                   var tree = new AceMenu
                   {
                       id = item.id,
                       text = item.Text,
                       Url=item.Url
                   };
                   //子列表
                   //  var chidrenList = orgs.Where(p => p.parentId == item.orgId).ToList();
                   tree.children = GetTreeChildNodes(orgs, item);
                   treeList.Add(tree);
               }
           }
           return treeList;
       }

       /// <summary>
       ///     获取当前节点的所有子点节
       /// </summary>
       /// <returns></returns>
       private List<AceMenu> GetTreeChildNodes(List<Sys_Menu> menus, Sys_Menu parentMenu)
       {
           var tsItems = new List<AceMenu>();
           var queryMenus =
               menus.Where(a => !string.IsNullOrEmpty(a.ParentNodeId) && a.ParentNodeId == parentMenu.id);
           if (queryMenus.Any())
           {
               //存在子节点
               queryMenus.ToList().ForEach(menu =>
               {
                   var tsParentNode = new AceMenu
                   {
                       id = menu.id,
                       text = menu.Text,
                       Url = menu.Url

                   };
                   //说明还要子节点 此时要递归
                   if (queryMenus.ToList().Count > 1)
                   {
                       tsParentNode.children = GetTreeChildNodes(menus, menu);
                   }


                   //图标
                   //switch (menu.file_type.ToLower())
                   //{
                   //    case ".pdf":
                   //        tsParentNode.spriteCssClass = "pdf";
                   //        break;
                   //    case ".doc":
                   //    case ".docx":
                   //        tsParentNode.spriteCssClass = "word";
                   //        break;
                   //}
                   tsItems.Add(tsParentNode);
               });
           }
           return tsItems;
       } 
    }
}
